import pymysql

# 打开数据库连接  注意文件名不要与pymysql 重名
db = pymysql.connect(host='localhost',
                     user='root',
                     password='root1234',
                     database='pytest',
                     cursorclass=pymysql.cursors.DictCursor)

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()


def find_by_id(id: int):
    global cursor
    sql = f'select * from student_info where id = {id}'
    cursor.execute(sql)
    return cursor.fetchone()


def insert_stu():
    global cursor,db
    sql = "insert into student_info( `name`, `age`, `id_card`, `sign`) \
          VALUES ( 'bb', 20, 'ala92kskla', '卡路里')"
    cursor.execute(sql)
    db.commit()
    print(f'insert_stu 插入结果:{cursor.rowcount}')

def close_dblink():
    global cursor
    cursor.close()
    db.close()


if __name__ == '__main__':
    result = find_by_id(1)
    print(f'查询结果: {type(result)}')

    insert_stu()

    close_dblink()
